package com.imooc.controller;

import com.imooc.enums.ErrorMsg;
import com.imooc.pojo.UserAddress;
import com.imooc.pojo.bo.AddressBO;
import com.imooc.service.AddressService;
import com.imooc.utils.CommonJsonResult;
import com.imooc.utils.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Example;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 逸尘风清
 * @date 2021/5/10
 * @time 9:55
 * 描述：
 */
@Api(value = "收货地址接口" , tags = {"收货地址相关接口"})
@RestController
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    @ApiOperation(value = "获取收货地址", notes = "获取收货地址")
    @GetMapping("getAddress")
    public CommonJsonResult getAddress(
            @ApiParam(value = "用户id", required = true)
            @RequestParam String userId){
        if (StringUtils.isBlank(userId)) {
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        List<UserAddress> userAddressList = addressService.getAddress(userId);

        return CommonJsonResult.ok(userAddressList);
    }

    @ApiOperation(value = "添加收货地址", notes = "添加收货地址")
    @PostMapping("addAddress")
    public CommonJsonResult addAddress(
            @ApiParam(value = "收货地址实体", required = true)
            @Valid
            @RequestBody AddressBO addressBO){
        UserAddress userAddress = addressService.addOrUpdateAddress(addressBO);
        return CommonJsonResult.ok(userAddress);
    }

    @ApiOperation(value = "更新收货地址",notes = "更新收货地址",httpMethod = "POST")
    @PostMapping("/updateAddress")
    public CommonJsonResult updateAddress(
            @ApiParam(value = "收货地址实体", required = true)
            @RequestBody AddressBO addressBO){

        if(StringUtils.isBlank(addressBO.getUserId()) || StringUtils.isBlank(addressBO.getAddressId())){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        //后端校验新增地址的一些参数是否符合条件
        CommonJsonResult checkRes = checkAddress(addressBO);
        if (checkRes.getStatus() != 200) {
            return checkRes;
        }
        UserAddress userAddress = addressService.addOrUpdateAddress(addressBO);
        return CommonJsonResult.ok(userAddress);
    }


    private CommonJsonResult checkAddress(AddressBO addressBO) {
        String receiver = addressBO.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            return CommonJsonResult.errorMsg("收货人不能为空");
        }
        if (receiver.length() > 12) {
            return CommonJsonResult.errorMsg("收货人姓名不能太长");
        }

        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)) {
            return CommonJsonResult.errorMsg("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return CommonJsonResult.errorMsg("收货人手机号长度不正确");
        }
        boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            return CommonJsonResult.errorMsg("收货人手机号格式不正确");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) ||
                StringUtils.isBlank(city) ||
                StringUtils.isBlank(district) ||
                StringUtils.isBlank(detail)) {
            return CommonJsonResult.errorMsg("收货地址信息不能为空");
        }

        return CommonJsonResult.ok();
    }

    @ApiOperation(value = "删除收货地址",notes = "删除收货地址",httpMethod = "POST")
    @PostMapping("/delAddress")
    public CommonJsonResult delAddress(
                @ApiParam(value = "用户ID", required = true)
                @RequestParam String userId,
                @ApiParam(value = "收获地址ID", required = true)
                @RequestParam String addressId
            )
    {
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        return addressService.delAddress(userId,addressId);
    }

    @ApiOperation(value = "设置默认地址",notes = "设置默认地址",httpMethod = "POST")
    @PostMapping("/setDefaultAddress")
    public CommonJsonResult setDefaultAddress(
            @ApiParam(value = "用户ID", required = true)
            @RequestParam String userId,
            @ApiParam(value = "收获地址ID", required = true)
            @RequestParam String addressId
    )
    {
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }

        return addressService.setDefaultAddress(userId,addressId);
    }
}
